<template>
    <!-- 对话框表单 -->
    <el-dialog class="ba-operate-dialog" :close-on-click-modal="false" :model-value="baTable.form.operate ? true : false" @close="baTable.toggleForm">
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    class="flex"
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    :disabled="baTable.form.operate == 'view'"
                >
                    <FormItem
                        :label="t('goods.GOODSID')"
                        type="string"
                        v-model="baTable.form.items!.GOODSID"
                        prop="GOODSID"
                        :input-attr="{ placeholder: t('保存时自动生成'),disabled:true }"
                    />
                    <!-- <FormItem
                        :label="t('goods.STATUS')"
                        type="radio"
                        v-model="baTable.form.items!.STATUS"
                        prop="STATUS"
                        :data="{ content: { 1: t('goods.STATUS 1') } }"
                        :input-attr="{ placeholder: t('Please select field', { field: t('goods.STATUS') }) }"
                    /> -->
                    <el-form-item label="有效性" prop="STATUS" >
                        <el-switch
                            v-model="baTable.form.items!.STATUS"
                            style="--el-switch-on-color: #13ce66;"
                            active-value="1"
                            inactive-value="0"
                        />
                    </el-form-item>
                    <FormItem
                        :label="t('goods.GOODS')"
                        type="string"
                        v-model="baTable.form.items!.GOODS"
                        prop="GOODS"
                        :input-attr="{ placeholder: t('Please input field', { field: t('goods.GOODS') }) }"
                    />
                    <FormItem
                        :label="t('goods.CORP')"
                        type="string"
                        v-model="baTable.form.items!.CORP"
                        prop="CORP"
                        :input-attr="{ placeholder: t('Please input field', { field: t('goods.CORP') }), disabled: true }"
                    />
                    <!-- <FormItem :label="t('goods.GOODSE')" type="string" v-model="baTable.form.items!.GOODSE" prop="GOODSE" :input-attr="{ placeholder: t('Please input field', { field: t('goods.GOODSE') }) }" /> -->
                    <FormItem
                        :label="t('goods.ename')"
                        type="string"
                        v-model="baTable.form.items!.ename"
                        prop="ename"
                        :input-attr="{ placeholder: t('Please input field', { field: t('goods.ename') }) }"
                    />
                    <FormItem
                        :label="t('goods.CIQNO')"
                        type="string"
                        v-model="baTable.form.items!.CIQNO"
                        prop="CIQNO"
                        :input-attr="{ placeholder: t('Please input field', { field: t('goods.CIQNO') }) }"
                    />
                    <!-- 危险品等级 -->
                    <FormItem
                        :label="t('goods.GOODSKIND')"
                        v-model="baTable.form.items!.GOODSKIND"
                        prop="GOODSKIND"
                        type="remoteSelect"
                        :input-attr="{
                            pk: 'code',
                            field: 'codename',
                            'remote-url': codelib + 'now?where=dangerlevel',
                        }"
                    />
                    <FormItem
                        :label="t('goods.TANKCBM')"
                        type="string"
                        prop="TANKCBM"
                        v-model.number="baTable.form.items!.TANKCBM"
                        :input-attr="{ step: '0.01', placeholder: t('Please input field', { field: t('goods.TANKCBM') }) }"
                    />
                    <!-- <FormItem :label="t('goods.GOODSPHYLUM')" type="string" v-model="baTable.form.items!.GOODSPHYLUM" prop="GOODSPHYLUM" :input-attr="{ placeholder: t('Please input field', { field: t('goods.GOODSPHYLUM') }) }" /> -->
                    <!-- <FormItem :label="t('goods.TYPE3')" type="string" v-model="baTable.form.items!.TYPE3" prop="TYPE3" :input-attr="{ placeholder: t('Please input field', { field: t('goods.TYPE3') }) }" /> -->
                    <FormItem
                        :label="t('goods.meltingpoint')"
                        type="number"
                        prop="meltingpoint"
                        v-model.number="baTable.form.items!.meltingpoint"
                        :input-attr="{ step: '0.01', placeholder: t('Please input field', { field: t('goods.meltingpoint') }) }"
                    />
                    <!-- 罐箱类型 -->
                    <FormItem
                        :label="t('goods.TANKTYPE')"
                        v-model="baTable.form.items!.TANKTYPE"
                        prop="TANKTYPE"
                        type="remoteSelect"
                        :input-attr="{
                            pk: 'code',
                            field: 'codename',
                            'remote-url': codelib + 'now?where=potkind',
                        }"
                    />
                    <FormItem
                        :label="t('goods.midu')"
                        type="number"
                        v-model="baTable.form.items!.midu"
                        prop="midu"
                        :input-attr="{step: '0.01', placeholder: t('Please input field', { field: t('goods.midu') }) }"
                    />
                    <!-- 包装类别 -->
                    <FormItem
                        :label="t('goods.pack')"
                        v-model="baTable.form.items!.pack"
                        prop="pack"
                        type="remoteSelect"
                        :input-attr="{
                            pk: 'code',
                            field: 'codename',
                            'remote-url': codelib + 'now?where=package',
                        }"
                    />
                    <!-- <FormItem
                        :label="t('goods.pack')"
                        type="string"
                        v-model="baTable.form.items!.pack"
                        prop="pack"
                        :input-attr="{ placeholder: t('Please input field', { field: t('goods.pack') }) }"
                    /> -->
                    <FormItem
                        :label="t('goods.boilingpoint')"
                        type="number"
                        prop="boilingpoint"
                        v-model.number="baTable.form.items!.boilingpoint"
                        :input-attr="{ step: '0.01',placeholder: t('Please input field', { field: t('goods.boilingpoint') }) }"
                    />
                    <FormItem
                        :label="t('goods.CASNO')"
                        type="string"
                        v-model="baTable.form.items!.CASNO"
                        prop="CASNO"
                        :input-attr="{ placeholder: t('Please input field', { field: t('goods.CASNO') }) }"
                    />
                    <FormItem
                        :label="t('goods.flashpoint')"
                        type="number"
                        prop="flashpoint"
                        v-model.number="baTable.form.items!.flashpoint"
                        :input-attr="{ step: '0.01',placeholder: t('Please input field', { field: t('goods.flashpoint') }) }"
                    />
                    <FormItem
                        :label="t('goods.unno')"
                        type="string"
                        v-model="baTable.form.items!.unno"
                        prop="unno"
                        :input-attr="{ placeholder: t('Please input field', { field: t('goods.unno') }) }"
                    />
                    <!-- <FormItem :label="t('goods.mpagroup')" type="string" v-model="baTable.form.items!.mpagroup" prop="mpagroup" :input-attr="{ placeholder: t('Please input field', { field: t('goods.mpagroup') }) }" /> -->
                    <!-- <FormItem :label="t('goods.remind')" type="string" v-model="baTable.form.items!.remind" prop="remind" :input-attr="{ placeholder: t('Please input field', { field: t('goods.remind') }) }" /> -->
                    <!-- <FormItem :label="t('goods.ems')" type="string" v-model="baTable.form.items!.ems" prop="ems" :input-attr="{ placeholder: t('Please input field', { field: t('goods.ems') }) }" /> -->
                    <!-- <FormItem :label="t('goods.mfag')" type="string" v-model="baTable.form.items!.mfag" prop="mfag" :input-attr="{ placeholder: t('Please input field', { field: t('goods.mfag') }) }" /> -->
                    <!-- <FormItem :label="t('goods.page')" type="string" v-model="baTable.form.items!.page" prop="page" :input-attr="{ placeholder: t('Please input field', { field: t('goods.page') }) }" /> -->
                    <!-- <FormItem :label="t('goods.rmi')" type="string" v-model="baTable.form.items!.rmi" prop="rmi" :input-attr="{ placeholder: t('Please input field', { field: t('goods.rmi') }) }" /> -->
                    <FormItem                        
                        :label="t('goods.PRENO')"
                        type="textarea"
                        v-model="baTable.form.items!.PRENO"
                        prop="PRENO"
                        :input-attr="{ placeholder: t('Please input field', { field: t('goods.PRENO') }),disabled:true }"
                    />
                    <!-- <FormItem                        
                        :label="t('goods.ALLOWTYPE')"
                        type="textarea"
                        v-model="baTable.form.items!.ALLOWTYPE"
                        prop="ALLOWTYPE"
                        :input-attr="{ placeholder: t('Please input field', { field: t('goods.ALLOWTYPE') }) }"
                    /> -->
                    
                    <FormItem
                        :label="t('goods.ALLOWTYPE')"
                        v-model="baTable.form.items!.ALLOWTYPES"
                        prop="ALLOWTYPES"
                        type="remoteSelect"
                        :input-attr="{
                            pk: 'code',
                            multiple:true,
                            field: 'codename',
                            'remote-url': codelib + 'now?where=allowtanktype',
                            placeholder: t('Click Select'),
                        }"
                    />
                    <!-- 'remote-url':'admin/XBjhead/cantype', -->
                    <FormItem
                        style="width: 100%"
                        :label="t('goods.zhuyi')"
                        type="textarea"
                        v-model="baTable.form.items!.zhuyi"
                        prop="zhuyi"
                        :input-attr="{ placeholder: t('Please input field', { field: t('goods.zhuyi') }) }"
                    />
                    <FormItem
                        style="width: 100%"
                        :label="t('goods.REFITDEMO')"
                        type="textarea"
                        v-model="baTable.form.items!.REFITDEMO"
                        prop="REFITDEMO"
                        :input-attr="{ rows: 3, placeholder: t('Please input field', { field: t('goods.REFITDEMO') }) }"
                        @keyup.enter.stop=""
                        @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                    />
                    <FormItem
                        style="width: 100%"
                        :label="t('goods.PRESSUREDEMO')"
                        type="textarea"
                        v-model="baTable.form.items!.PRESSUREDEMO"
                        prop="PRESSUREDEMO"
                        :input-attr="{ rows: 3, placeholder: t('Please input field', { field: t('goods.PRESSUREDEMO') }) }"
                        @keyup.enter.stop=""
                        @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                    />
                    <FormItem
                        style="width: 100%"
                        :label="t('goods.REMARK')"
                        type="textarea"
                        v-model="baTable.form.items!.REMARK"
                        prop="REMARK"
                        :input-attr="{ placeholder: t('Please input field', { field: t('goods.REMARK') }) }"
                    />
                    <FormItem :label="t('goods.MSDSFILES')" type="files" v-model="baTable.form.items!.MSDSFILES" prop="MSDSFILES" />
                    <FormItem
                        :label="t('goods.OP')"
                        type="string"
                        v-model="baTable.form.items!.OP"
                        prop="OP"
                        :input-attr="{ placeholder: t('当前登录人'),disabled:true }"
                    />
                    <FormItem
                        :label="t('goods.MSDSDATE')"
                        type="datetime"
                        v-model="baTable.form.items!.MSDSDATE"
                        prop="MSDSDATE"
                        :input-attr="{ placeholder: t('Please select field', { field: t('goods.MSDSDATE') }) }"
                    />
                    <FormItem
                        :label="t('goods.OPTIME')"
                        type="datetime"
                        v-model="baTable.form.items!.OPTIME"
                        prop="OPTIME"
                        :input-attr="{ placeholder: t('当前时间'),disabled:true }"
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer v-if="baTable.form.operate !== 'view'">
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject ,watchEffect} from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { useRoute } from 'vue-router'
import { codelib } from '/@/api/controllerUrls'



const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

console.log(baTable)

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    GOODS: [buildValidatorData({ name: 'required', title: t('goods.GOODS') })],
    ALLOWTYPES: [buildValidatorData({ name: 'required', title: t('goods.ALLOWTYPES') })],
    OPTIME: [buildValidatorData({ name: 'date', message: t('Please enter the correct field', { field: t('goods.OPTIME') }) })],
    MSDSDATE: [buildValidatorData({ name: 'date', message: t('Please enter the correct field', { field: t('goods.MSDSDATE') }) })],
})


const route = useRoute()
const type = route.name === 'corplib/customer'? 1:2

const companyName = inject('companyName') as String
watchEffect(()=>{
    if(baTable.form.operate == 'add'){
        baTable.form.items!.CORP = companyName.value
        baTable.form.items!.STATUS = '1'
    }
})

const handleSave= ()=>{
    baTable.form.items!.type = type
    baTable.onSubmit(formRef.value)
}

</script>

<style scoped lang="scss"></style>
